package com.gzsxy.JUC.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//lock
public class Demo02 {
    public static void main(String[] args) {
        phone phone = new phone();

        new Thread(() -> {
            phone.sms();
        }, "A").start();


        new Thread(() -> {
            phone.call();
        }, "B").start();
    }
}


class phone2 {
    Lock lock = new ReentrantLock();

    public void sms() {
        lock.lock();   //细节问题 lock.lock（） lock.unlock();
        //lock锁必须配对不然会死锁

        try {
            System.out.println(Thread.currentThread().getName() + "打电话");
            call();//这里也有锁
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void call() {
        lock.lock();

        try {
            System.out.println(Thread.currentThread().getName() + "发短信");
            call();//这里也有锁
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}